package com.heo.util;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;

public class RedisUtil<K, V>
{
  private static RedisTemplate<String, Object> redisTemplate;

  private static final long timeout = 5;

  /**
   * 添加对象到redis
   * 
   * @param key
   * @param value
   */
  public static void addValue(String key, Object value)
  {
    addValue(key, value, timeout);
  }

  /**
   * 添加对象到redis
   * 
   * @param key
   * @param value
   * @param timeout
   */
  public static void addValue(String key, Object value, long timeout)
  {
//    redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
	 redisTemplate.opsForValue().set(key, value);
     redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
  }

  /**
   * 通过一个正则表达式，取出符合条件的key
   * @param pattern
   * @return
   */
  public static Set<String> keys(String pattern)
  {
    return redisTemplate.keys(pattern);
  }

  /**
   * 刷新key的过期时间
   * 
   * @param key
   * @param timeout
   */
  public static void expire(String key, long timeout)
  {
    redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
  }

  public static Object getValue(String key)
  {
    return redisTemplate.opsForValue().get(key);
  }
  
  public static List<Object> multiGet(List<String> key)
  {
    return redisTemplate.opsForValue().multiGet(key);
  }

  public static void remove(String key)
  {
    redisTemplate.delete(key);
  }

  public RedisTemplate<String, Object> getRedisTemplate()
  {
    return redisTemplate;
  }

  public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate)
  {
    RedisUtil.redisTemplate = redisTemplate;
  }
}
